//
//  GoodsVerification.swift
//  eemall
//
//  Created by zhuxietong on 2018/9/30.
//  Copyright © 2018 zhuxietong. All rights reserved.
//

import Foundation
import soapp
import Eelay
import AsyncDisplayKit

public class GoodsVerification {
    
    
    
    public enum Status:String{
        case used = "1" //已使用
        case unuse = "2" //未使用
        case unget = "3" //未取货
        case timeout = "4" //已过期，未使用
        
        case unkown = ""
        
        var code_status_img:UIImage?{
            get{
                switch self {
                case .used:
                    return UIImage(named: "task_used_tag")
                case .unuse:
                    return UIImage(named: "task_valied_tag")
                case .timeout:
                    return UIImage(named: "task_invalied_tag")
                default:
                    return nil
                }
            }
        }
    }
    
    public var orderDetail = NSMutableDictionary(){
        didSet{
            self.order = DataNode.path(node: "order", orderDetail, value: NSMutableDictionary())
        }
    }
    
    public var order = NSMutableDictionary(){
        didSet{
        }
    }
    public init(detail:NSMutableDictionary) {

        self.orderDetail = detail
        self.order = DataNode.path(node: "order", detail, value: NSMutableDictionary())
        self.showVerification = order["showverify",false.tag] == true.tag
        self.checkStatus()
    }
    
    
    public var status:Status = Status.unkown
    
    
    public func checkStatus() ->Void{    
        self.status = .unkown
        if self.showVerification{
            if order["verified",false.tag] == true.tag{
                self.status = .used
            }else{
                let timeStr = order["verifyendtime",""]
                if timeStr.len > 0{
                    let time = stampToTime(timeStr)
                    switch time.timeSince(Date()).0{
                    case .ealier:
                        self.status = .timeout
                    default:
                        break
                    }
                }
            }
            
            switch status{
            case .unkown:
                if order["dispatchtype",false.tag] == true.tag
                {
                    self.status = .unget
                }
                else{
                    self.status = .unuse
                }
            default:
                break
            }
        }
    }
    
    
//            {if $order['showverify']  && !$order['isonlyverifygoods']}
//            {if $order['status']>0 || $order['paytime'] > 0}
//            {foreach $order['verifyinfo'] $v}
//                {if $order['dispatchtype']==1}自提码{else}消费码{/if}: {$v['verifycode']}
//                    {if $v['verified']}
//                        已使用
//                        {elseif $order['verifyendtime'] > 0 && $order['verifyendtime'] < time()}
//                        已过期
//                            {elseif $order['dispatchtype']}
//                        未取货
//                            {elseif $order['verifytype']==1}
//                        剩余{$order['goods'][0]['total']-count($order['vs'])} 次
//                            {else}
//                        未使用
//                            {/if}
//                        {/if}
//                        {/foreach}
//                            {else}
//                        付款后可见!
//                            {/if}
//                        {/if}
    
    public var showVerification:Bool = false
}


extension GoodsVerification{
    
    class TitleCell:JoTableCell{
        let titleL = UILabel()
        let tagBt = UIButton()
        override func addLayoutRules() {
            contentView.eelay = [
                [titleL,[ee.T.L.B,[12,AppTheme.edge,-12]],[ee.R,-80]],
                [tagBt,[ee.R,-AppTheme.edge],[titleL,ee.Y]],
                [__line,[ee.L.B.R],"\(TP.separatorHeight)"],
            ]
            tagBt.ui.font(size: 12).ncolor(hex: "#888").ntitle(title: "")
            contentView.backgroundColor = UIColor(shex: "\(AppTheme.main_hex)22")
            __line.backgroundColor = UIColor.separator_color
            titleL.ui.bfont12.cl_666.text("线下消费门店".loc)
            tagBt.addTarget(self, action: #selector(seeAll), for: .touchUpInside)
        }
        
        @objc func seeAll() {
            let ctr = OrderOfflineAddress()
            ctr.mo.model = DataNode.path(node: "__model", mo.model, value: NSMutableDictionary())
            mo.controller?.navigationController?.pushViewController(ctr, animated: true)
        }
        override func mo_model_reload() {
            let total = mo.model[int:"total",0]
            if mo.model[int:"total",0] > 1{
                tagBt.alpha = 1
                tagBt.isEnabled = true
                tagBt.ui.ntitle(title: "共[XX]家门店".replaceVars(values: "\(total)")).ncolor(hex: "#222").font(size: 10)
            }
            else{
                tagBt.alpha = 0
                tagBt.isEnabled = false
            }
        }
    }
    
    class AddressCell: JoTableCell {
        let storeL = UILabel()
        let addressL = UILabel()
        let callBt = UIButton()
        
        let line1 = UIView()
        let line2 = UIView()
        
        override func addLayoutRules() {
            contentView.eelay = [
                [storeL,[ee.T.L.R,[14,AppTheme.edge,-75]]],
                [addressL,[ee.B.L.R,[-14,AppTheme.edge,-75]],[storeL,ee.B,ee.T,10]],
                [line2,[addressL,ee.R,ee.L,10],[ee.Y],"40",TP.separatorHeight],
                [callBt,[line2,ee.Y],[line2,ee.R,ee.L,10]],
                [__line,[ee.L.B.R],"\(TP.separatorHeight)"]
            ]
            
            __line.backgroundColor = UIColor._separator
            line2.backgroundColor = UIColor._separator
            _ = callBt.ui.padding(insets: [4,10]).nimage(UIImage(named: "bodadianhua"))
            callBt.addTarget(self, action: #selector(callAction), for: .touchUpInside)
        }
        
        override func mo_model_reload() {
            let address = mo.model["address",""]
            let name =  mo.model.possible("storename","merchname", default: "核销门店".loc)
            storeL.attributedText = name.ui.font12.color(color: ._foreground).astring
            addressL.attributedText = address.ui.font11.color(color: .color(light: "#666", dark: "#aaa")).astring
        

        }
        
        @objc func callAction() {
            
            __app.call(serverPhone: mo.model["tel",__app.serverPhone])
        }
    }
    

    class AddressCell1: JoCellNode {
        
        
        let storeL = JoTextNode()
        let addressL = JoTextNode()
        let callBt = ASButtonNode()
        
        let line1 = ASDisplayNode()
        let line2 = ASDisplayNode()
        
        override func addLayoutRules() {
            separatorHeight = TP.separatorHeight
            separatorColor = UIColor.table_bg_color
            
            callBt.addTarget(self, action: #selector(callAction), forControlEvents: .touchUpInside)
            
        }
        
     
        @objc func callAction() {
            
            
            
            __app.call(serverPhone: mo.model["tel",__app.serverPhone])
        }
        
        override func mo_model_reload() {
            //            separatorTopHeight = 8
            //            separatorTopColor = UIColor.table_bg_color
            //            separatorHeight = 8
            //            separatorColor = UIColor.table_bg_color
            backgroundColor = .white
            
            line1.backgroundColor = UIColor._separator
            line2.backgroundColor = UIColor._separator
            let address = mo.model["address",""]
            let name =  mo.model["merchname",""]
            
            
            storeL.attributedText = name.ui.font12.color(color: ._foreground).astring
            addressL.attributedText = address.ui.font11.color(color: .color(light: "#666", dark: "#aaa")).astring
            
            _ = callBt.ui.padding(insets: [4,10]).nimage(UIImage(named: "bodadianhua"))
            
        }
        override func specGetter() -> _Spec {
            let spec = _Spec.stack([[10,AppTheme.edge],"v|8",&-.stretch], childs: [
                _Spec.stack(["h|16",_nowrap,&-.center], childs: [
                    _Spec.stack(["v|8",_nowrap,&-.stretch,_grow(1),_shrink(1)], childs: [
                        storeL.Spec,
                        addressL.Spec
                        ]),
                    line2[TP.separatorHeight,"\(40)"],
                    callBt.Spec
                    ]),
                ]
            )
            return spec
        }
    }
}
